<template>
  <basic-container>
    <avue-crud
      :option="option"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      :table-loading="loading"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @selection-change="selectionChange"
      :key="timer"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true" v-if="permission.lineSideWarehouseInventory_export"
          >导出</el-button
        >
      </template>
      <template slot="menu" slot-scope="{ row }">
        <el-button type="text" size="small" icon="el-icon-view" v-if="permission.lineSideWarehouseInventory_view" @click="detailEvent(row)">流水详情</el-button>
      </template>
    </avue-crud>
    <el-dialog append-to-body :close-on-click-modal="false" :visible.sync="detailDialog.visible" title="流水详情" custom-class="lineSideWarehouseInventoryDialog">
      <div class="common-modal-sub-title">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        <h1 class="avue-group__title">物料信息</h1>
      </div>
      <avue-form ref="properties" v-model="properties" :option="propertiesOption"> </avue-form>
      <div class="common-modal-sub-title">
        <i class="el-icon-vertical-bar avue-group__icon"></i>
        <h1 class="avue-group__title">流水详情</h1>
      </div>
      <avue-crud
        ref="detailCrud"
        :option="detailDialog.option"
        :data="detailDialog.data"
        :page.sync="detailDialog.page"
        @current-change="currentDetailChange"
        @size-change="sizeDetailChange"
        :table-loading="detailDialog.loading"
        @search-change="detailSearchChange"
        @search-reset="detailSearchReset"
      >
        <!-- <template slot="menuRight">
          <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true" v-if="permission.lineSideWarehouseInventory_export"
            >导出</el-button
          >
        </template> -->
      </avue-crud>
      <div slot="footer" class="avue-dialog__footer">
        <el-button size="small" icon="el-icon-circle-close" @click="detailDialog.visible = false">取消</el-button>
      </div>
    </el-dialog>
    <export-comp ref="exportComp" page-api-url="mes-warehouse/inventory/line/side/page" :option="option" :page="page" :query="query" file-name="线边仓库存"> </export-comp>
  </basic-container>
</template>

<script>
import { getList, getDetail, getLockList } from '@/api/warehouse/lineSideWarehouseInventory';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp';
import userTableConfig from '@/components/user-table-config/index.vue';

export default {
  components: {
    exportComp,
    userTableConfig
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  data() {
    return {
      pageCode: 'LineSideWarehouseInventory',
      form: {},
      query: {},
      loading: true,
      timer: new Date().getTime(),
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      params: {},
      selectionList: [],
      data: [],
      option: {
        height: 'calc(100vh - 360px)',
        columnBtn: false,
        calcHeight: 30,
        tip: false,
        selection: true,
        refreshBtn: true, //刷新按钮是否显示
        searchBtn: true, // 显示查询的按钮
        searchSpan: 4,
        border: true,
        dialogClickModal: false,
        addBtn: false,
        delBtn: false,
        editBtn: false,
        viewBtn: false,
        width: '100',
        searchLabelWidth: 80,
        searchMenuPosition: 'right',
        searchMenuSpan: 4,
        searchShow: true,
        index: true,
        indexLabel: '序号',
        menuWidth: 100,
        column: [
          {
            label: '仓库名称',
            prop: 'warehouseName',
            overHidden: true,
            search: true
          },
          {
            label: '物料名称',
            prop: 'partName',
            overHidden: true,
            search: true
          },
          {
            label: '物料编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '物料规格',
            prop: 'partSpec',
            search: true
          },
          {
            label: '库存总量',
            prop: 'quantity'
          }
        ]
      },
      properties: {},
      propertiesOption: {
        //单据FORM配置
        labelWidth: '110',
        submitBtn: false,
        emptyBtn: false,
        detail: true,
        span: 8,
        column: [
          {
            label: '仓库名称',
            prop: 'warehouseName'
          },
          {
            label: '物料名称',
            prop: 'partName'
          },
          {
            label: '物料编码',
            prop: 'partNo'
          },
          {
            label: '物料规格',
            prop: 'partSpec'
          },
          {
            label: '库存总量',
            prop: 'quantity'
          }
        ]
      },
      detailDialog: {
        visible: false,
        loading: false,
        query: {},
        data: [],
        option: {
          height: '300px',
          calcHeight: 30,
          delBtn: false,
          editBtn: false,
          addBtn: false,
          searchBtn: true,
          searchShow: true,
          searchSpan: 12,
          searchMenuSpan: 6,
          searchMenuPosition: 'right',
          refreshBtn: false, //刷新按钮是否显示
          columnBtn: false, //控制表格列是否显示按钮是否显示
          border: true,
          page: true,
          index: true,
          indexLabel: '序号',
          menu: false, //表格操作栏是否显示
          column: [
            {
              label: '时间范围',
              prop: 'queryTime',
              searchType: 'datetimerange',
              format: 'yyyy-MM-dd HH:mm:ss',
              valueFormat: 'yyyy-MM-dd HH:mm:ss',
              startPlaceholder: '请选择开始时间',
              endPlaceholder: '请选择结束时间',
              hide: true,
              search: true
            },
            {
              label: '关联单据号',
              prop: 'sheetNo'
            },
            {
              label: '变更类型',
              prop: 'changeType',
              dicData: [
                {
                  label: '入库',
                  value: 'INBOUND'
                },
                {
                  label: '出库',
                  value: 'OUTBOUND'
                },
                {
                  label: '调拨',
                  value: 'TRANSFER'
                }
              ]
            },
            {
              label: '原始库存总量',
              prop: 'originalQuantity'
            },
            {
              label: '本次变更数量',
              prop: 'changeQuantity'
            },
            {
              label: '变更后库存数量',
              prop: 'changedQuantity'
            },
            {
              label: '操作人',
              prop: 'operatorName'
            },
            {
              label: '变更时间',
              prop: 'changeTime',
              type: 'datetime',
              format: 'yyyy-MM-dd HH:mm:ss',
              valueFormat: 'yyyy-MM-dd HH:mm:ss',
              searchRange: true,
              span: 4
            }
          ]
        },
        page: {
          pageSize: 20,
          currentPage: 1,
          total: 0
        }
      }
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {};
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    beforeOpen(done, type) {
      done();
    },
    searchReset(params, done) {
      this.query = {};
      this.page.currentPage = 1;
      this.page.pageSize = 20;
      this.onLoad(this.page, this.query);
    },
    searchChange(params, done) {
      if (params.inventoryName) {
        params.inventoryId = params.inventoryName;
        delete params.inventoryName;
      }

      if (params.areaName) {
        params.areaId = params.areaName;
        delete params.areaName;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.params);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    refreshChange() {
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      this.data = [{ id: 1 }];
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        .then((res) => {
          const data = res.data.data;
          this.data = data.records;
          this.page.total = data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    detailEvent(row) {
      this.detailDialog.visible = true;
      this.properties = structuredClone(row);
      delete this.detailDialog.query.queryStartTime;
      delete this.detailDialog.query.queryEndTime;
      this.$set(this.detailDialog.query, 'inventoryLineSideId', row.id);
      this.getStorageDetail(this.detailDialog.page);
    },
    getStorageDetail(page, params = {}) {
      this.detailDialog.loading = true;
      getDetail(page.currentPage, page.pageSize, Object.assign(params, this.detailDialog.query))
        .then((res) => {
          let data = res.data.data;
          this.detailDialog.data = data.records;
          this.detailDialog.page.total = data.total;
        })
        .finally(() => {
          this.detailDialog.loading = false;
        });
    },
    currentDetailChange(currentPage) {
      this.detailDialog.page.currentPage = currentPage;
      this.getStorageDetail(this.detailDialog.page);
    },
    sizeDetailChange(pageSize) {
      this.detailDialog.page.pageSize = pageSize;
      this.getStorageDetail(this.detailDialog.page);
    },
    detailSearchReset(params, done) {
      delete this.detailDialog.query.queryStartTime;
      delete this.detailDialog.query.queryEndTime;
      this.detailDialog.page.currentPage = 1;
      this.detailDialog.page.pageSize = 20;
      this.getStorageDetail(this.detailDialog.page, this.detailDialog.query);
    },
    detailSearchChange(params, done) {
      if (params.queryTime) {
        params.queryStartTime = params.queryTime[0];
        params.queryEndTime = params.queryTime[1];
        delete params.queryTime;
      }
      this.$set(this.detailDialog.query, 'queryStartTime', params.queryStartTime);
      this.$set(this.detailDialog.query, 'queryEndTime', params.queryEndTime);
      this.detailDialog.page.currentPage = 1;
      this.getStorageDetail(this.detailDialog.page, this.detailDialog.query);
      done();
    },
    newKey() {
      this.timer = new Date().getTime();
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .lineSideWarehouseInventoryDialog {
  .el-dialog__body {
    padding: 0 20px 20px;
  }
  .avue-crud__menu {
    display: none;
  }
  .common-modal-sub-title {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: bold;
    margin: 10px 0;
  }
}
</style>
